﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Common;
using System.Data.SQLite;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ExpenseTracker.Entity;
using ExpenseTracker.Controller;

namespace ExpenseTracker
{
    public partial class Workspace : Form
    {
        private static object _synclock = new object();
        private static ExpenseTracker.Entity.Account _account;
        private CategoryList _categoryList;
        private ExpenseList _expenseList;
        private ExpenseReport _expenseReport;

        public Workspace()
        {
            InitializeComponent();
        }

        public static ExpenseTracker.Entity.Account Account
        {
            get
            {
                lock (_synclock)
                {
                    if (_account != null)
                    {
                        return _account;
                    }
                    return null;
                }
            }
            set
            {
                lock (_synclock)
                {
                    _account = value;
                }
            }
        }

        protected int CategoryListCount
        {
            get
            {
                int count = 0;
                foreach (Form form in this.MdiChildren)
                {
                    if (form is CategoryList)
                    {
                        count++;
                    }
                }
                return count;
            }
        }

        protected int ExpenseListCount
        {
            get
            {
                int count = 0;
                foreach (Form form in this.MdiChildren)
                {
                    if (form is ExpenseList)
                    {
                        count++;
                    }
                }
                return count;
            }
        }

        protected int ExpenseReportCount
        {
            get
            {
                int count = 0;
                foreach (Form form in this.MdiChildren)
                {
                    if (form is ExpenseReport)
                    {
                        count++;
                    }
                }
                return count;
            }
        }

        public string StatusLabel
        {
            get { return toolStripStatusLabel.Text; }
            set { toolStripStatusLabel.Text = value; }
        }

        private bool WarnAccountChange()
        {
            bool result = false;
            string message = "You need to close all open category and expense windows to create a new account or change an account. Are you sure?";

            if (this.MdiChildren.Length > 0)
            {
                if (MessageBox.Show(message, Configuration.Instance.ApplicationName, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    foreach (Form form in this.MdiChildren)
                    {
                        form.Close();
                    }
                    result = true;
                }
            }
            else
            {
                result = true;
            }

            return result;
        }

        private void ShowNewForm(object sender, EventArgs e)
        {
            if (WarnAccountChange())
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string filter = string.Format("Database Files (*.{0})|*.{0}", Configuration.Instance.DatabaseFileExtension);
                saveFileDialog.Filter = filter;
                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    Account = new Account(saveFileDialog.FileName);
                    this.Text = Configuration.Instance.ApplicationName + " : " + Account.DatabaseFileName;

                    CategoryController categoryController = new CategoryController(Account);
                    categoryController.CreateTable();
                    ExpenseController expenseController = new ExpenseController(Account);
                    expenseController.CreateTable();
                }
            }
        }

        private void OpenFile(object sender, EventArgs e)
        {
            if (WarnAccountChange())
            {
                OpenFileDialog openFileDialog = new OpenFileDialog();
                openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                string filter = string.Format("Database Files (*.{0})|*.{0}", Configuration.Instance.DatabaseFileExtension);
                openFileDialog.Filter = filter;
                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    Account = new Account(openFileDialog.FileName);
                    this.Text = Configuration.Instance.ApplicationName + " : " + Account.DatabaseFileName;

                    listCategoriesToolStripMenuItem_Click(this, null);
                    //listExpensesToolStripMenuItem_Click(this, null);
                    expenseReportToolStripMenuItem_Click(this, null);
                }
            }
        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            string filter = string.Format("Database Files (*.{0})|*.{0}", Configuration.Instance.DatabaseFileExtension);
            saveFileDialog.Filter = filter;
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                File.Copy(Account.DatabaseFilePath, saveFileDialog.FileName, true);
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void newCategoryToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Account != null)
            {
                CategoryForm childForm = new CategoryForm(Account);
                childForm.Name = Guid.NewGuid().ToString();
                childForm.MdiParent = this;
                childForm.Text = "Create Category";
                childForm.FormAction = FormAction.Create;
                if (_categoryList != null)
                {
                    childForm.OnCategoryFormSaved += new CategoryForm.CategoryFormSaved(_categoryList.PopulateCategoryList);
                }
                childForm.Show();
            }
        }

        private void listCategoriesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Account != null)
            {
                if (CategoryListCount == 0)
                {
                    _categoryList = new CategoryList(Account);
                    _categoryList.Name = Guid.NewGuid().ToString();
                    _categoryList.MdiParent = this;
                    _categoryList.Show();
                }    
            }
        }

        private void newExpenseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Account != null)
            {
                ExpenseForm childForm = new ExpenseForm(Account);
                childForm.Name = Guid.NewGuid().ToString();
                childForm.MdiParent = this;
                childForm.Text = "Create Expense";
                childForm.FormAction = FormAction.Create;
                if (_expenseReport != null)
                {
                    childForm.OnExpenseFormSaved += new ExpenseForm.ExpenseFormSaved(_expenseReport.PopulateExpenseList);
                }
                childForm.Show();
            }
        }

        private void listExpensesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Account != null)
            {
                if (ExpenseListCount == 0)
                {
                    _expenseList = new ExpenseList(Account);
                    _expenseList.Name = Guid.NewGuid().ToString();
                    _expenseList.MdiParent = this;
                    _expenseList.Show();
                }
            }
        }

        private void tsbNewExpense_Click(object sender, EventArgs e)
        {
            newExpenseToolStripMenuItem_Click(sender, e);
        }

        private void tsbNewCategory_Click(object sender, EventArgs e)
        {
            newCategoryToolStripMenuItem_Click(sender, e);
        }

        private void tsbListCategories_Click(object sender, EventArgs e)
        {
            listCategoriesToolStripMenuItem_Click(sender, e);
        }

        private void tsbListExpenses_Click(object sender, EventArgs e)
        {
            listExpensesToolStripMenuItem_Click(sender, e);
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutExpenseTracker childForm = new AboutExpenseTracker();
            childForm.Name = Guid.NewGuid().ToString();
            childForm.MdiParent = this;
            childForm.Show();           
        }

        private void expenseReportToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Account != null)
            {
                if (ExpenseReportCount == 0)
                {
                    _expenseReport = new ExpenseReport(Account);
                    _expenseReport.Name = Guid.NewGuid().ToString();
                    _expenseReport.MdiParent = this;
                    _expenseReport.Show();
                }
            }
        }

        private void tsbExpenseReport_Click(object sender, EventArgs e)
        {
            expenseReportToolStripMenuItem_Click(sender, e);
        }
    }
}
